package edu.hcmus.sow.web.discussion;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import edu.hcmus.sow.domain.Forum;
import edu.hcmus.sow.domain.Topic;
import edu.hcmus.sow.domain.TopicComment;
import edu.hcmus.sow.service.DiscussionService;

@Controller
public class DiscussionController {
	@Autowired
	DiscussionService discussionService;

	@RequestMapping("/discussion")
	public ModelAndView discussion() {
		ModelAndView mav = new ModelAndView();
		// get all forum
		List<Forum> forums = discussionService.getAllForum();
		mav.addObject("forums", forums);

		// get list number topics
		List<Integer> topicsNumber = discussionService.getNumberTopicsOfForum();
		mav.addObject("topicsNumber", topicsNumber);

		// get list latest topics
		List<Topic> latestTopics = discussionService.getLatestTopicList();
		mav.addObject("latestTopics", latestTopics);

		mav.setViewName("discussion");
		return mav;
	}

	@RequestMapping("/discussion/add")
	public ModelAndView addBox() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("addBox");
		return mav;
	}

	@RequestMapping("discussion/add/save")
	public String createNewBox(@RequestParam("title") String name,
			@RequestParam("description") String description,
			HttpServletResponse response) {
	   response.setContentType("text/html;charset=UTF-8");
		discussionService.addNewForum(name, description);
		return "redirect:/discussion";
	}

	@RequestMapping("/box/{id}/view")
	public ModelAndView box(@PathVariable("id") Integer forumID) { // forumID ==
																	// boxID
		ModelAndView mav = new ModelAndView();

		// get forum
		Forum forum = discussionService.fiForumById(forumID);
		mav.addObject("forum", forum);

		// get all topics of Box
		List<Topic> topics = discussionService.getTopicsByForumID(forumID);
		mav.addObject("topics", topics);

		// get comment number of topics
		List<Integer> commentsNumber = discussionService
				.getNumberCommentOfTopics(forumID);
		mav.addObject("commentsNumber", commentsNumber);

		mav.setViewName("box");
		return mav;
	}

	@RequestMapping("/box/{id}/update")
	public String updateBox (
	      @PathVariable("id") Integer forumID,
	      @RequestParam("name") String name,
	      @RequestParam("description") String description){
	   discussionService.updateBox(forumID,  name,  description);
	   return "redirect:/box/" + forumID + "/view";
	}
	@RequestMapping("/box/{id}/add")
	public ModelAndView addTopic(@PathVariable("id") Integer forumID) {
		ModelAndView mav = new ModelAndView();
		// get forum
		Forum forum = discussionService.fiForumById(forumID);
		mav.addObject("forum", forum);

		mav.setViewName("addTopic");
		return mav;
	}

	@RequestMapping("/box/{id}/delete")
   public ModelAndView deleteBox(@PathVariable("id") Integer forumID) {
      ModelAndView mav = new ModelAndView();
      // get forum
      discussionService.deleteForum(forumID);
      // get all forum
      List<Forum> forums = discussionService.getAllForum();
      mav.addObject("forums", forums);

      // get list number topics
      List<Integer> topicsNumber = discussionService.getNumberTopicsOfForum();
      mav.addObject("topicsNumber", topicsNumber);

      // get list latest topics
      List<Topic> latestTopics = discussionService.getLatestTopicList();
      mav.addObject("latestTopics", latestTopics);
      mav.setViewName("discussion");
      return mav;
   }
	
	@RequestMapping("/box/{id}/add/save")
	public String createNewTopic(@PathVariable("id") Integer forumID,
			@RequestParam("title") String name,
			@RequestParam("description") String description) {

		discussionService.addTopic(name, description, forumID);
		return "redirect:/box/" + forumID + "/view";
	}

	@RequestMapping("/topic/{id}/view")
	public ModelAndView topic(@PathVariable("id") Integer topicID) {

		ModelAndView mav = new ModelAndView();

		// get current topic
		Topic topic = discussionService.getTopicByID(topicID);
		mav.addObject("topic", topic);

		// get current box
		Forum forum = discussionService.getForumByTopicID(topicID);
		mav.addObject("forum", forum);

		// get comment list
		List<TopicComment> commentList = discussionService.getTopicCommentsByTopicID(topicID);
		mav.addObject("commentsList", commentList);
		
		mav.setViewName("topic");
		return mav;
	}

	@RequestMapping("/topic/{id}/edit")
	public ModelAndView editTopic(@PathVariable("id") Integer topicID) {

		ModelAndView mav = new ModelAndView();

		Topic topic = discussionService.getTopicByID(topicID);
		mav.addObject("topic", topic);

		mav.setViewName("editTopic");
		return mav;
	}

	@RequestMapping("/topic/{id}/update")
	public String updateTopic(@PathVariable("id") Integer topicID,
			@RequestParam("title") String title,
			@RequestParam("description") String description) {

		discussionService.updateTopic(topicID, title, description);
		return "redirect:/topic/" + topicID + "/view";
	}

	@RequestMapping("/topic/{id}/delete")
	public String deleteTopic(@PathVariable("id") Integer topicID) {

		// get forum ID to return when delete completed
		Forum forum = discussionService.getForumByTopicID(topicID);
		Integer forumID = forum.getId();

		// delete topic
		discussionService.deleteTopic(topicID);

		return "redirect:/box/" + forumID + "/view";
	}

	// Ajax add new topic comment
	@RequestMapping(value = "/ajax/topic/addComment", method = RequestMethod.POST)
	public ModelAndView ajaxAddTopicComment(
			@RequestParam("topicID") Integer topicID,
			@RequestParam("content") String content) {
		ModelAndView mav = new ModelAndView();

		// add new topic comment
		discussionService.addTopicComment(topicID, content);

		// get comment list
		List<TopicComment> commentList = discussionService
				.getTopicCommentsByTopicID(topicID);
		mav.addObject("commentsList", commentList);

		mav.setViewName("controls/c_discussionComments");
		return mav;
	}

	// update topic comment
	@RequestMapping("/topic/comment/{id}/update")
	public String updateTopicComment(@PathVariable("id") Integer commentID,
			@RequestParam("comment") String content) {

		// get topic ID
		int topicID = discussionService.getTopicByTopicCommentID(commentID);

		// update comment of topic
		discussionService.updateTopicComment(commentID, content);

		return "redirect:/topic/" + topicID + "/view";
	}

	@RequestMapping("/topic/comment/{id}/delete")
	public String deleteTopicComment(@PathVariable("id") Integer commentID) {

		// get topic ID
		int topicID = discussionService.getTopicByTopicCommentID(commentID);

		// update comment of topic
		discussionService.deleteTopicComment(commentID);

		return "redirect:/topic/" + topicID + "/view";
	}
}
